function doubleByteToInt(lowByte: number, highByte: number): number {
  return ((highByte & 0xff) << 8) | (lowByte & 0xff);
}

function convertPositionToDegrees(position: number): number {
  return (position * 300.0) / 1023.0;
}

function convertSpeedToRPM(speed: number): number {
  return (speed * 62.0) / 1023.0;
}

export function parseAngleFromBytes(lowByte: number, highByte: number): number {
  const position = doubleByteToInt(lowByte, highByte);
  return convertPositionToDegrees(position);
}

export function parseSpeedFromBytes(lowByte: number, highByte: number): number {
  const speed = doubleByteToInt(lowByte, highByte);
  return convertSpeedToRPM(speed);
}

export function parseVoltage(number: number): number {
  return number * 0.1;
}

export enum AlarmLEDConfig {
  INSTRUCTION_ERROR = 1 << 6, // 指令错误
  OVERLOAD = 1 << 5, // 过载
  CHECKSUM_ERROR = 1 << 4, // 校验和错误
  RANGE_ERROR = 1 << 3, // 指令超范围
  OVERHEAT = 1 << 2, // 过热
  ANGLE_ERROR = 1 << 1, // 超过角度范围
  VOLTAGE_ERROR = 1 << 0, // 超过电压范围
}

// 中文标签映射
export const AlarmLEDLabels: Record<AlarmLEDConfig, string> = {
  [AlarmLEDConfig.INSTRUCTION_ERROR]: '指令错误',
  [AlarmLEDConfig.OVERLOAD]: '过载',
  [AlarmLEDConfig.CHECKSUM_ERROR]: '校验和错误',
  [AlarmLEDConfig.RANGE_ERROR]: '指令超范围',
  [AlarmLEDConfig.OVERHEAT]: '过热',
  [AlarmLEDConfig.ANGLE_ERROR]: '超过角度范围',
  [AlarmLEDConfig.VOLTAGE_ERROR]: '超过电压范围',
};

export function parseLedSettings(data: number): AlarmLEDConfig[] {
  const leds: AlarmLEDConfig[] = [];
  Object.values(AlarmLEDConfig)
    .filter((value) => typeof value === 'number')
    .forEach((value) => {
      if ((data & (value as number)) !== 0) {
        leds.push(value as AlarmLEDConfig);
      }
    });
  return leds;
}

export function combineLedSettings(leds: AlarmLEDConfig[]): number {
  return leds.reduce((acc, cur) => acc | cur, 0);
}

export enum AlarmShutdownConfig {
  INSTRUCTION_ERROR = 1 << 6, // 指令错误
  CHECKSUM_ERROR = 1 << 4, // 校验和错误
  RANGE_ERROR = 1 << 3, // 指令超范围
  OVERHEAT = 1 << 2, // 过热
  ANGLE_ERROR = 1 << 1, // 角度超范围
  VOLTAGE_ERROR = 1 << 0, // 电压超范围
  NONE = 0, // 不启用卸载保护
}

// 中文标签映射
export const AlarmShutdownLabels: Record<AlarmShutdownConfig, string> = {
  [AlarmShutdownConfig.INSTRUCTION_ERROR]: '指令错误',
  [AlarmShutdownConfig.CHECKSUM_ERROR]: '校验和错误',
  [AlarmShutdownConfig.RANGE_ERROR]: '指令超范围',
  [AlarmShutdownConfig.OVERHEAT]: '过热',
  [AlarmShutdownConfig.ANGLE_ERROR]: '角度超范围',
  [AlarmShutdownConfig.VOLTAGE_ERROR]: '电压超范围',
  [AlarmShutdownConfig.NONE]: '不启用卸载保护',
};

export function parseShutdownSettings(data: number): AlarmShutdownConfig[] {
  const shutdowns: AlarmShutdownConfig[] = [];
  Object.values(AlarmShutdownConfig)
    .filter(
      (value) => typeof value === 'number' && value !== AlarmShutdownConfig.NONE
    )
    .forEach((value) => {
      if ((data & (value as number)) !== 0) {
        shutdowns.push(value as AlarmShutdownConfig);
      }
    });
  return shutdowns;
}

export function combineShutdownSettings(
  shutdowns: AlarmShutdownConfig[]
): number {
  if (shutdowns.length === 0) {
    return AlarmShutdownConfig.NONE;
  }
  return shutdowns.reduce((acc, cur) => acc | cur, 0);
}
